package com.whut.mobilesafe.engine;

import android.app.ActivityManager;
import android.app.ActivityManager.*;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.*;
import android.graphics.drawable.Drawable;
import android.os.Debug;
import android.text.TextUtils;
import android.text.format.Formatter;

import com.whut.mobilesafe.R;
import com.whut.mobilesafe.domain.ProcessInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * 作    者:yubo.xiaoyubo
 * 时    间：2015/12/29 15:58
 * 版    本：1.0
 * 描    述：正在运行中的进程信息   总内存和剩余内存
 */
public class ProcessInfoProvider {

    public static int getRunningProcessCount(Context context) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> runningAppProcesses = activityManager.getRunningAppProcesses();
        return runningAppProcesses.size();
    }

    public static ArrayList<ProcessInfo> getRunningProcessInfo(Context context) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

        ArrayList<ActivityManager.RunningAppProcessInfo> runningAppProcesses = (ArrayList<ActivityManager.RunningAppProcessInfo>) activityManager.getRunningAppProcesses();
        ArrayList<ProcessInfo> processinfos = new ArrayList<>();

        for (ActivityManager.RunningAppProcessInfo runningAppProcessInfo : runningAppProcesses) {
            ProcessInfo pInfo = new ProcessInfo();

            String packageName = runningAppProcessInfo.processName;

            String name ="";//应用名
            try {
                //图标
                PackageManager packageManager = context.getPackageManager();
                ApplicationInfo applicationInfo = packageManager.getApplicationInfo(packageName, android.content.pm.PackageManager.GET_UNINSTALLED_PACKAGES);
                Drawable icon = applicationInfo.loadIcon(packageManager);
                pInfo.setIcon(icon);
                name = applicationInfo.loadLabel(packageManager).toString();

                int flags = applicationInfo.flags;
                if((flags & ApplicationInfo.FLAG_SYSTEM) == ApplicationInfo.FLAG_SYSTEM){
                    pInfo.setIsUser(false);
                }else{
                    pInfo.setIsUser(true);
                }
                pInfo.setName(name);//应用名字
            } catch (android.content.pm.PackageManager.NameNotFoundException e) {
               pInfo.setName(packageName);
                pInfo.setIcon(context.getResources().getDrawable(R.drawable.img1));
                pInfo.setIsUser(false);
            }
            //进程大小
            int pid = runningAppProcessInfo.pid;
            Debug.MemoryInfo[] processMemoryInfo = activityManager.getProcessMemoryInfo(new int[]{pid});
            long memory = processMemoryInfo[0].getTotalPrivateDirty() * 1024;// 获取当前进程占用内存大小
            pInfo.memory = memory;
            // 单位是kb
            pInfo.setSize(Formatter.formatFileSize(context,memory));

            processinfos.add(pInfo);
        }

        return processinfos;
    }


    public static ArrayList<ProcessInfo> getRunnningProcesses(Context ctx) {
        ActivityManager am = (ActivityManager) ctx
                .getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> runningAppProcesses = am
                .getRunningAppProcesses();// 获取运行中进程集合

        PackageManager pm = ctx.getPackageManager();
        ArrayList<ProcessInfo> list = new ArrayList<ProcessInfo>();
        for (RunningAppProcessInfo runningAppProcessInfo : runningAppProcesses) {
            ProcessInfo info = new ProcessInfo();
            String packageName = runningAppProcessInfo.processName;// 包名
            info.name = packageName;

            int pid = runningAppProcessInfo.pid;// 进程id

            android.os.Debug.MemoryInfo[] processMemoryInfo = am
                    .getProcessMemoryInfo(new int[]{pid});// 根据pid返回内存信息

            long memory = processMemoryInfo[0].getTotalPrivateDirty() * 1024;// 获取当前进程占用内存大小
            // 单位是kb
            info.size = Formatter.formatFileSize(ctx,memory);
            try {
                ApplicationInfo applicationInfo = pm.getApplicationInfo(
                        packageName, 0);// 根据包名获取相关应用的信息
                String name = applicationInfo.loadLabel(pm).toString();
                Drawable icon = applicationInfo.loadIcon(pm);

                int flags = applicationInfo.flags;
                if ((flags & ApplicationInfo.FLAG_SYSTEM) == ApplicationInfo.FLAG_SYSTEM) {
                    // 系统进程
                    info.isUser = false;
                } else {
                    // 用户进程
                    info.isUser = true;
                }

                info.name = name;
                info.icon = icon;
            } catch (NameNotFoundException e) {
                // 某些系统进程没有名称和图标,会走此异常
                info.name = info.name;
                info.icon = ctx.getResources().getDrawable(
                        R.drawable.img1);
                info.isUser = false;
                e.printStackTrace();
            }

            list.add(info);
        }

        return list;
    }


    public static void killAll(Context context){
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        ArrayList<String> killList = new ArrayList<>();
        List<RunningAppProcessInfo> runningAppProcesses = activityManager.getRunningAppProcesses();
        for(RunningAppProcessInfo info:runningAppProcesses){
            if(info.processName!=null){
                killList.add(info.processName);//将报名保存下来
            }
        }
        for(String packageName:killList){
            activityManager.killBackgroundProcesses(packageName);
        }
    }
}
